import { Controller } from "../../mvc/core/Controller";
import { IPlayerLockController } from "./interfaces/IPlayerLockController";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { LockInfoData } from "../view/lock/LockInfoData";
import { ELockStatus } from "../../../Message/Game/ELockStatus";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { IView } from "../../mvc/interfaces/IView";
import { PlayerLockModule } from "../../../modules/PlayerLockModule";
import { ILockSetPasswordWindow } from "../../../modules/interfaces/ILockSetPasswordWindow";
import { LockSetPasswordWindow } from "../view/lock/LockSetPasswordWindow";
import { ILockFindPasswordWindow } from "../../../modules/interfaces/ILockFindPasswordWindow";
import { LockFindPasswordWindow } from "../view/lock/LockFindPasswordWindow";
import { ILockSetUnlockWindow } from "../../../modules/interfaces/ILockSetUnlockWindow";
import { LockSetUnlockWindow } from "../view/lock/LockSetUnlockWindow";
import { ILockModifyPasswordWindow } from "../../../modules/interfaces/ILockModifyPasswordWindow";
import { LockModifyPasswordWindow } from "../view/lock/LockModifyPasswordWindow";
import { ILockCancelProtectWindow } from "../../../modules/interfaces/ILockCancelProtectWindow";
import { LockCancelProtectWindow } from "../view/lock/LockCancelProtectWindow";
import { NumberKeyboardSprite } from "../view/lock/NumberKeyboardSprite";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SPlayerLockStatus } from "../../../Message/Game/SPlayerLockStatus";
import { GameController } from "../mvc/GameController";
import { SPlayerLockError } from "../../../Message/Game/SPlayerLockError";
import { IPlayerLockModule } from "../../../modules/interfaces/IPlayerLockModule";
//class PlayerLockController
    
    export  class PlayerLockController extends Controller implements IPlayerLockController
    {
       

        /* internal  */onLockSetLockStatusHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            var loc2=loc1[0];
            var loc3=loc1[1];
            GameProxy.roleProxy.setLockStatus(loc2, loc3);
            return;
        }

        /* internal  */onLockSetLockStatusSuccessHandler(arg1: DataEvent): void
        {
            if (LockInfoData.lockStatus.value() != ELockStatus._ELockStatusUnLocked) 
            {
                MsgManager.showRollTipsMsg(Language.getString(35557));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(35556));
            }
            if (!this.setUnLockWindow.isHide) 
            {
                this.setUnLockWindow.hide();
            }
            return;
        }

        /* internal  */onLockModifyPasswordHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            GameProxy.roleProxy.modifyLock(loc1);
            return;
        }

        /* internal  */onLockModifyPasswordSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(35558));
            if (!this.modifyPasswordWindow.isHide) 
            {
                this.modifyPasswordWindow.hide();
            }
            return;
        }

        /* internal  */onLockClearPasswordHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            GameProxy.roleProxy.clearLock(loc1);
            return;
        }

        /* internal  */LockClearPasswordSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(35559));
            if (!this.cancelProtectWindow.isHide) 
            {
                this.cancelProtectWindow.hide();
            }
            return;
        }

        /* internal  */onLockGetQuestionHandler(arg1: DataEvent=null): void
        {
            if (LockInfoData.questionOne == "" || LockInfoData.questionTwo == "") 
            {
                GameProxy.roleProxy.getLockQuestions();
            }
            return;
        }

        /* internal  */onNumberKeyboardVisibleHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                this.numberKeyboardSp.show(this.setPasswordWindow.x + this.setPasswordWindow.width, this.setPasswordWindow.y + 150);
            }
            else 
            {
                this.numberKeyboardSp.hide();
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._playerLockModule == null) 
            {
                this._playerLockModule = new PlayerLockModule();
            }
            return this._playerLockModule;
        }

        public get setPasswordWindow(): ILockSetPasswordWindow
        {
            if (this._setPasswordWindow == null) 
            {
                this._setPasswordWindow = new LockSetPasswordWindow();
            }
            return this._setPasswordWindow;
        }

        public get findPasswordWindow(): ILockFindPasswordWindow
        {
            if (this._findPasswordWindow == null) 
            {
                this._findPasswordWindow = new LockFindPasswordWindow();
            }
            return this._findPasswordWindow;
        }

        public get setUnLockWindow(): ILockSetUnlockWindow
        {
            if (this._setUnLockWindow == null) 
            {
                this._setUnLockWindow = new LockSetUnlockWindow();
            }
            return this._setUnLockWindow;
        }

        public get modifyPasswordWindow(): ILockModifyPasswordWindow
        {
            if (this._modifyPasswordWindow == null) 
            {
                this._modifyPasswordWindow = new LockModifyPasswordWindow();
            }
            return this._modifyPasswordWindow;
        }

        /* internal  */get cancelProtectWindow(): ILockCancelProtectWindow
        {
            if (this._cancelProtectWindow == null) 
            {
                this._cancelProtectWindow = new LockCancelProtectWindow();
            }
            return this._cancelProtectWindow;
        }

        /* internal  */get numberKeyboardSp(): NumberKeyboardSprite
        {
            if (this._numberKeyboardSp == null) 
            {
                this._numberKeyboardSp = new NumberKeyboardSprite();
            }
            return this._numberKeyboardSp;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerLockStatus, this.onPlayerLockStatusHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerLockError, this.onPlayerLockErrorHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePlayerLockIsLocked, this.onPlayerLockIsLockedHandler);
            Dispatcher.addEventListener(EventName.LockModuleOpenOrHide, this.onLockModuleOpenOrHideHandler);
            Dispatcher.addEventListener(EventName.LockSetPassWordOpen, this.onLockSetPassWordOpenHandler);
            Dispatcher.addEventListener(EventName.LockCancelProtectOpen, this.onLockCancelProtectOpenHandler);
            Dispatcher.addEventListener(EventName.LockSetUnLockOpen, this.onLockSetUnLockOpenHandler);
            Dispatcher.addEventListener(EventName.LockModifyPassWordOpen, this.onLockModifyPassWordOpenHandler);
            Dispatcher.addEventListener(EventName.LockFindPassWordOpen, this.onLockFindPassWordOpenHandler);
            Dispatcher.addEventListener(EventName.LockToActive, this.onLockToActiveHandler);
            Dispatcher.addEventListener(EventName.LockGetQuestionSuccess, this.onLockGetQuestionSuccessHandler);
            Dispatcher.addEventListener(EventName.LockAnswerQuestions, this.onLockAnswerQuestionsHandler);
            Dispatcher.addEventListener(EventName.LockAnswerQuestionCorrect, this.onLockAnswerQuestionCorrectHandler);
            Dispatcher.addEventListener(EventName.LockAnswerSetLock, this.onLockAnswerSetLockHandler);
            Dispatcher.addEventListener(EventName.LockAnswerSetLockSuccess, this.LockAnswerSetLockSuccessHandler);
            Dispatcher.addEventListener(EventName.LockSetLockStatus, this.onLockSetLockStatusHandler);
            Dispatcher.addEventListener(EventName.LockSetLockStatusSuccess, this.onLockSetLockStatusSuccessHandler);
            Dispatcher.addEventListener(EventName.LockModifyPassword, this.onLockModifyPasswordHandler);
            Dispatcher.addEventListener(EventName.LockModifyPasswordSuccess, this.onLockModifyPasswordSuccessHandler);
            Dispatcher.addEventListener(EventName.LockClearPassword, this.onLockClearPasswordHandler);
            Dispatcher.addEventListener(EventName.LockClearPasswordSuccess, this.LockClearPasswordSuccessHandler);
            Dispatcher.addEventListener(EventName.LockGetQuestion, this.onLockGetQuestionHandler);
            Dispatcher.addEventListener(EventName.LockNumberKeyboardVisible, this.onNumberKeyboardVisibleHandler);
            return;
        }

        /* internal  */onPlayerLockStatusHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPlayerLockStatus;
            LockInfoData.lockStatus = loc1.status;
            GameController.gameui.getGameui().updateLockStatus();
            if (this._playerLockModule != null) 
            {
                this._playerLockModule.updateStatus();
            }
            if (this._isActiveLock) 
            {
                this._isActiveLock = false;
                MsgManager.showRollTipsMsg(Language.getString(35554));
            }
            if (!(this._setPasswordWindow == null) && !this._setPasswordWindow.isHide) 
            {
                this.setPasswordWindow.hide();
            }
            return;
        }

        /* internal  */onPlayerLockErrorHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPlayerLockError;
            LockInfoData.sPlayerLockError = loc1;
            if (this._cancelProtectWindow != null) 
            {
                this.cancelProtectWindow.setLeftTimes();
            }
            if (this._findPasswordWindow != null) 
            {
                this.findPasswordWindow.setLeftTimes();
            }
            if (this._modifyPasswordWindow != null) 
            {
                this.modifyPasswordWindow.setLeftTimes();
            }
            return;
        }

        /* internal  */onPlayerLockIsLockedHandler(arg1: MessageBlock): void
        {
            this.setUnLockWindow.show();
            this.numberKeyboardSp.show(this.setUnLockWindow.x + this.setUnLockWindow.width, this.setUnLockWindow.y - 20);
            return;
        }

        /* internal  */onLockModuleOpenOrHideHandler(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onLockSetPassWordOpenHandler(arg1: DataEvent): void
        {
            this.setPasswordWindow.show();
            return;
        }

        /* internal  */onLockCancelProtectOpenHandler(arg1: DataEvent): void
        {
            this.onLockGetQuestionHandler();
            this.cancelProtectWindow.show();
            return;
        }

        /* internal  */onLockSetUnLockOpenHandler(arg1: DataEvent): void
        {
            this.setUnLockWindow.show();
            this.numberKeyboardSp.show(this.setUnLockWindow.x + this.setUnLockWindow.width, this.setUnLockWindow.y - 20);
            return;
        }

        /* internal  */onLockModifyPassWordOpenHandler(arg1: DataEvent): void
        {
            this.modifyPasswordWindow.show();
            this.numberKeyboardSp.show(this.modifyPasswordWindow.x + this.modifyPasswordWindow.width, this.modifyPasswordWindow.y + 50);
            return;
        }

        /* internal  */onLockFindPassWordOpenHandler(arg1: DataEvent): void
        {
            this.onLockGetQuestionHandler();
            this.findPasswordWindow.show();
            return;
        }

        /* internal  */onLockToActiveHandler(arg1: DataEvent): void
        {
            var loc1=[];
            if (LockInfoData.lockStatus.value() != ELockStatus._ELockStatusNoPassword) 
            {
                if (LockInfoData.lockStatus.value() == ELockStatus._ELockStatusNULL) 
                {
                    this._isActiveLock = true;
                    loc1.push(LockInfoData.password);
                    loc1.push(LockInfoData.questionOne);
                    loc1.push(LockInfoData.answerOne);
                    loc1.push(LockInfoData.questionTwo);
                    loc1.push(LockInfoData.answerTwo);
                    GameProxy.roleProxy.activeLock(loc1);
                }
            }
            else 
            {
                loc1.push(LockInfoData.password);
                loc1.push(LockInfoData.answerOne);
                loc1.push(LockInfoData.answerTwo);
                GameProxy.roleProxy.setLock(loc1);
            }
            return;
        }

        /* internal  */onLockGetQuestionSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            LockInfoData.questionOne = loc1[0];
            LockInfoData.questionTwo = loc1[1];
            if (this._findPasswordWindow != null) 
            {
                this.findPasswordWindow.setQuestion();
            }
            if (this._cancelProtectWindow != null) 
            {
                this.cancelProtectWindow.setQuestion();
            }
            if (!(this._setPasswordWindow == null) && LockInfoData.settingStep == 3) 
            {
                this.setPasswordWindow.setQuestion();
            }
            return;
        }

        /* internal  */onLockAnswerQuestionsHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            GameProxy.roleProxy.answerLockQuestions(loc1);
            return;
        }

        /* internal  */onLockAnswerQuestionCorrectHandler(arg1: DataEvent): void
        {
            this.findPasswordWindow.answerQuestionCorrect();
            return;
        }

        /* internal  */onLockAnswerSetLockHandler(arg1: DataEvent): void
        {
            var loc1=[];
            loc1.push(LockInfoData.password);
            loc1.push(LockInfoData.answerOne);
            loc1.push(LockInfoData.answerTwo);
            GameProxy.roleProxy.setLock(loc1);
            return;
        }

        /* internal  */LockAnswerSetLockSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(35555));
            if (!this.setPasswordWindow.isHide) 
            {
                this.setPasswordWindow.hide();
            }
            if (!this.findPasswordWindow.isHide) 
            {
                this.findPasswordWindow.hide();
            }
            return;
        }

        private /* var */_playerLockModule: IPlayerLockModule;

        private /* var */_setPasswordWindow: ILockSetPasswordWindow;

        private /* var */_findPasswordWindow: ILockFindPasswordWindow;

        private /* var */_setUnLockWindow: ILockSetUnlockWindow;

        private /* var */_modifyPasswordWindow: ILockModifyPasswordWindow;

        private /* var */_cancelProtectWindow: ILockCancelProtectWindow;

        private /* var */_numberKeyboardSp: NumberKeyboardSprite;

        private /* var */_isActiveLock: Boolean=false;
    }
